#!/usr/bin/php
<?php
/**
 * This file is part of php-server-class.
 * 
 * php-server-class is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * php-server-class is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with php-server-class.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @package php-server-class
 * @author Rick Barenthin <dunst0@gmail.com>
 * @copyright Copyright (c) 2010, Rick Barenthin
 * @license http://www.gnu.org/licenses/ GNU General Public License v3
 * @version 0.0.3
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
set_time_limit(0);
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Europe/Berlin');


require_once 'package-server/package.server.php';


$Server = new \Server\Server ('localhost', 80);
$Server->enableDebugOutput();
$Server->setRequestTermination("\r\n\r\n");
$Server->setMaxRequestCount(10);
$Server->setKeepAliveTimeOut(115);
$Server->registerRequestHandler(
    function ($rawRequest)
    {
        $content = '';
        $text  = 'HTTP/1.1 200 OK' . "\r\n";
        $text .= 'Sever: ' . 'myPHPSever/0.0(alpha)' . "\r\n";
        $text .= 'Date: ' . gmdate('D, d M Y H:i:s') . ' GMT' . "\r\n";
        $text .= 'Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT' . "\r\n";
        $text .= 'Cache-Control: ' . 'no-cache' . "\r\n";
        $text .= 'Content-Type: ' . 'text/html; charset=UTF-8' . "\r\n";
        $text .= 'Content-Length: ' . strlen($content) . "\r\n";
        $text .= 'Connection: keep-alive' . "\r\n";
        $text .= "\r\n";
        
        return (object) Array(
            'response' => $text,
            'keepAlive' => TRUE,
            'accessInfo' => 'GET "/" "Me"'
        );
    }
);
$Server->start();
while (TRUE)
    $Server->loop();
$Server->stop();

?>